﻿using DataTurnClient.Model;
using DataTurnClient.ViewModel;
using MetroFramework.Controls;
using ReactiveUI;
using Splat;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataTurnClient.Controls
{
    public partial class SetView : UserControl, IViewFor<SetViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (SetViewModel)value;
        }

        public SetViewModel ViewModel { get; set; }
        public SetView()
        {
            InitializeComponent();
            InitBindings();
            switch1.CheckedChanged += Switch1_CheckedChanged;
            switch2.CheckedChanged += Switch2_CheckedChanged;
        }

        private void InitBindings()
        {
            ViewModel = new SetViewModel();
            this.WhenActivated(d =>
            {

                this.Bind(ViewModel,
                    vm => vm.Timer,
                    v => v.inputNumber1.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Log,
                    v => v.inputNumber2.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Url,
                    v => v.input1.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.SafeCode,
                    v => v.input2.Text
                    ).DisposeWith(d);

                this.badge1.Events().Click.Do(x =>
                {
                    this.Dispose();
                }).Subscribe().DisposeWith(d);

                this.Events().Load.Do(x =>
                {
                    ViewModel.SelectSetCommand.Execute().Subscribe(result =>
                    {
                        if (result.Success)
                        {
                            ViewModel.SettingList = result.Data;
                        }
                        else
                        {
                            MessageBox.Show(result.Message);
                        }
                    });
                }).Subscribe();

                ViewModel.WhenAnyValue(vm => vm.On).Do(x =>
                {
                    switch2.Checked = x;
                }).Subscribe().DisposeWith(d);

                ViewModel.WhenAnyValue(vm => vm.Run).Do(x =>
                {
                    switch1.Checked = x;
                }).Subscribe().DisposeWith(d);

                button1.Events().Click.Do(x =>
                {
                    ViewModel.SaveSetCommand.Execute().Subscribe(result =>
                    {
                        if (!result.Success || result.Data.Where(y => y == 0).Count() > 0)
                        {
                            MessageBox.Show(result.Message);
                        }
                    });
                }).Subscribe().DisposeWith(d);

            });
        }


        private void Switch2_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            ViewModel.On = switch2.Checked;
        }

        private void Switch1_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            ViewModel.Run = switch1.Checked;
        }

    }
}
